package hadoop.topN.question1;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class StuReducer1 extends Reducer<Text, DoubleWritable,CourseBean, NullWritable> {
    CourseBean cb=new CourseBean();
    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
        //求各个科目的平均分
        double sum=0;
        int count=0;
        for(DoubleWritable value:values){
            sum+=value.get();
            count++;
        }
        double courseAvg=sum/count;

        //封装courseBean对象
        cb.setCourseName(key.toString());
        cb.setStuNum(count);
        cb.setCourseAvg(courseAvg);

        //写入上下文（结果）
        context.write(cb,NullWritable.get());
    }
}
